package com.hj.ipproxy.handler;

import java.util.List;
import java.util.Set;

import com.hj.ipproxy.bean.HttpEntity;
import com.hj.ipproxy.bean.ProxyIp;
import com.hj.ipproxy.service.ProxyIpServiceImpl;
import com.hj.ipproxy.util.ProxyIpUtil;
import com.hj.tools.StringUtil;

/**
 * http://mianfeidaili.ttju.cn/
 * @author HJ
 */
public class MianfeidailiTtjuCnHandler extends FetchIpHandler {
	private int port = 80;
	@Override
	protected void initHttpEntity(HttpEntity ht) {
	}

	@Override
	protected String[] getUrls() {
		String url = "http://mianfeidaili.ttju.cn/getAgent.php?" +
				"uCard={0}&pCard={1}&Number={2}&Area={3}&Operators={4}&port={5}&list={6}";
		String uCard = StringUtil.urlEncode("请填写赞助卡号,可不填写", "gb2312");
		String pCard = StringUtil.urlEncode("请填写赞助卡密,可不填写", "gb2312");
		//提取数量
		String number = "300";
		//电信、联通、移动
		String operators = StringUtil.urlEncode("联通", "gb2312");
		//北京、广东、辽宁
		String area = StringUtil.urlEncode("广东", "gb2312");
		//(白名单限制端口)Wlist, (黑名单过滤端口 )Blist
		String list = "Wlist";
		String[] urls = {
				StringUtil.format(url, uCard,pCard,number,area,operators,port+"",list)
		};
		return urls;
	}

	@Override
	protected List<String[]> getProxyIps(HttpEntity ht) {
		List<String[]> list = null;
		if (ht.send()) {
			list = StringUtil.matchAll(ht.getBody(), "("+ProxyIpUtil.REGX_IP+"):"+port);
		}
		return list;
	}

	@Override
	protected void doParse(Set<ProxyIp> iplist, List<String[]> list, ProxyIpServiceImpl proxyIpSev) {
		System.out.println(Thread.currentThread().getName()+"MianfeidailiTtjuCnHandler总共抓取了("+list.size()+")条IP");
		int port = 80;
		for (String[] ipsinfos : list) {
			String ip=ipsinfos[1];
			ProxyIp pi = new ProxyIp();
			pi.setIp(ip);
			pi.setPort(port);
			pi.setType("HTTP");
			checkAndFilterProxyIpInfo(pi, iplist, proxyIpSev);
		}
		
	}

}
